
package com.monlyu.tag.extend;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.monlyu.tag.BaseTag;
import com.monlyu.util.DateUtil;
import com.monlyu.util.FtlUtil;
import com.monlyu.util.StringUtil;

/********************************************************************************
 * <b>功能描述☞</b>日期标签,可用选择用时间<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.tag.extend.DateTag.java<br>
 * <b>开发日期☞</b>2010-2-22 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 ********************************************************************************/
public class DateTag extends BaseTag {

    private String name;

    private boolean time;

    private String cssClass;

    private String value;

    private String minDate;

    private String maxDate;

    private boolean readonly;

    public void setName(String name){
        this.name = name;
    }

    public void setTime(boolean time){
        this.time = time;
    }

    public void setCssClass(String cssClass){
        this.cssClass = cssClass;
    }

    public void setValue(String value){
        this.value = value;
    }

    public void setMinDate(String minDate){
        this.minDate = minDate;
    }

    public void setMaxDate(String maxDate){
        this.maxDate = maxDate;
    }

    public void setReadonly(boolean readonly){
        this.readonly = readonly;
    }

    @Override
    public int doStartTag(){
        try {
            write2Page(FtlUtil.render2Ftl(toMap(), "date.ftl"));
        }
        catch (Exception e) {
            id = null;
            name = null;
            time = false;
            cssClass = null;
            minDate = null;
            maxDate = null;
            readonly = false;
        }
        return SKIP_BODY;
    }

    private Map<String, Object> toMap(){
        Map<String, Object> map = Maps.newHashMap();
        if (!StringUtil.isTrimEmpty(id)) {
            map.put("id", id);
        }
        if (!StringUtil.isTrimEmpty(name)) {
            map.put("name", name);
        }
        if (!StringUtil.isTrimEmpty(cssClass)) {
            map.put("class", cssClass);
        }
        if (!StringUtil.isTrimEmpty(readonly) && readonly) {
            map.put("isReadonly", readonly);
        }
        if (StringUtil.isEmpty(value)) {
            Date date = (Date) getValueFromValueStack(name);
            if (date != null) {
                if (time) {
                    map.put("value", DateUtil.format(date, DateUtil.DATETIMENOS_PATTERN));
                } else {
                    map.put("value", DateUtil.format(date));
                }
            } else {
                if (!time) {
                    map.put("value", DateUtil.now2Str());
                }
            }
        } else {
            map.put("value", value);
        }
        map.put("setting", generSetting());
        return map;
    }

    /**
     * 组装setting
     * 
     * @since 2010-2-22
     */

    private String generSetting(){
        List<String> args = Lists.newArrayList();
        args.add("showOn: 'both'");
        args.add("buttonImageOnly: true");
        args.add("buttonImage: '" + COMMON_IMAGES_DIR + "optImg/ical.png'");
        if (!StringUtil.isTrimEmpty(maxDate)) {
            args.add("maxDate:" + maxDate);
        }
        if (!StringUtil.isTrimEmpty(minDate)) {
            args.add("minDate:" + minDate);
        }
        if (!StringUtil.isTrimEmpty(time) && time) {
            args.add("dateFormat:" + "'yy-mm-dd h:i'");
            args.add("isTime:" + time);
        }

        return "{" + args.toString().substring(1, args.toString().length() - 1) + "}";
    }
}
